/**
 * Created by Eric.Dou on 2/24/14.
 */
define({
    views: ['menu'],
    views_menu: {
        lazyLoad: true,
        target: '#menuBar',
        layout: {
            belongTo: '#westMenuDiv'
        },
        listener: function ($) {
            console.log("------------------- into listener");
           var  click_event = 'click';
            $(".nav-list").on(click_event, function (h) {
                var g = $(h.target).closest("a");
                if (!g || g.length == 0) {
                    return
                }else
                {
                    var href= g.attr('href')
                    if('#'!=href)
                    {
                        $('#frame').layout('panel', 'center').panel('refresh', href);
                    }
                }
                c = $("#sidebar").hasClass("menu-min");
                if (!g.hasClass("dropdown-toggle")) {
                    if (c && click_event == "tap" && g.get(0).parentNode.parentNode == this) {
                        var i = g.find(".menu-text").get(0);
                        if (h.target != i && !$.contains(i, h.target)) {
                            return false
                        }
                    }
                    return
                }
                var f = g.next().get(0);
                if (typeof(f) !== 'undefined' && !$(f).is(":visible")) {
                    var d = $(f.parentNode).closest("ul");
                    if (c && d.hasClass("nav-list")) {
                        return
                    }
                    d.find("> .open > .submenu").each(function () {
                        if (this != f && !$(this.parentNode).hasClass("active")) {
                            $(this).slideUp(200).parent().removeClass("open")
                        }
                    })
                } else {
                }
                if (c && $(f.parentNode.parentNode).hasClass("nav-list")) {
                    return false
                }
                $(f).slideToggle(200).parent().toggleClass("open");
                return false
            });

        },
        html: {
            lazyLoad: {
                lazyLoadContent: '<p>Loading....</p>',
                url: '/api/bo/nav',
                data: {
                    needTotal: false
                },
                method: 'getJSON',
                handle: function ($, data, status,xhr) {
                    var firstLevelNavTemplete = '<ul class="nav nav-list"> @subnav </ul>';
                    var liTemplete = '<li class="@liClass"><a href="@href" class="@aclass">#icon #content</a> @subnav </li>';
                    var subNavTemplete = '<ul class="submenu">  @subnav </ul>';
                    var subFlagTemp = '<b class="arrow icon-angle-down"></b>';

                    function parentLi(format, data, href, content) {
                        var str = makeLi(format, data, href, content);
                        if (typeof(data.icon) != 'undefined') {
                            str = str.replace('#icon', ' <span class="menu-text"> <i class= "' + data.icon + '" ></i>  ' + data.text + ' </span>');
                        } else {
                            str = str.replace('#icon', '<span class="menu-text"> ' + data.text + ' </span>');
                        }
                        return str;
                    }

                    function subLi(format, data, href, content) {
                        var str = makeLi(format, data, href, content);
                        if (typeof(data.icon) != 'undefined') {
                            str = str.replace('#icon', '<i class="icon-double-angle-right"></i>  <i class= "' + data.icon + '" ></i>  ' + data.text);
                        } else {
                            str = str.replace('#icon', '<i class="icon-double-angle-right"></i>  ' + data.text);
                        }
                        return str;
                    }

                    function makeLi(format, data, href, content) {
                        var str = format.replace('@liClass', '');

                        str = str.replace('@href', href);
                        if (typeof(data.children) != 'undefined') {
                            str = str.replace('@aclass', 'dropdown-toggle');
                        } else {
                            str = str.replace('@aclass', '');
                        }
                        if (typeof(content) == 'undefined') {
                            content = '';
                        }
                        str = str.replace('#content', content);
                        return str;
                    }

                    var menulist = '';

                    $.each(data, function (i, n) {
                        var newStr = '';
                        if (typeof(n.children) != 'undefined') {
                            newStr = parentLi(liTemplete, n, '#', subFlagTemp);
                            console.log(newStr);
                            var subMenu = '';
                            $.each(n.children, function (j, o) {
                                var cc = subLi(liTemplete, o, o.value);
                                //just have two level .
                                cc = cc.replace('@subnav', '');
                                subMenu += cc;
                            });
                            newStr = newStr.replace('@subnav', subNavTemplete.replace('@subnav', subMenu));
                        } else {
                            newStr = makeLi(liTemplete, n, '#');
                        }
                        menulist += newStr;
                    });
                    var startMenu = parentLi(liTemplete, {text:'开始',icon:'icon-rocket  bigger-150'}, '#');
                    menulist =startMenu.replace('@subnav', '')+menulist;
                    menulist = firstLevelNavTemplete.replace('@subnav', menulist);
                    return menulist;
                }
            }
        }
    }
})
;